package com.service.kefu;

import com.model.kefu.Kefu;
import com.util.WeixinUtil;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

/**
 * Created by Administrator on 2016/11/10.
 */
@Service("kefuService")
public class KefuServiceImpl implements KefuService {
    private static Logger log = LoggerFactory.getLogger(KefuServiceImpl.class);

    //添加客服
    private String kfaccount_add_url ="https://api.weixin.qq.com/customservice/kefu/add?access_token=ACCESS_TOKEN";

    //修改客服信息
    private String kfaccount_update_url="https://api.weixin.qq.com/customservice/kefu/update?access_token=ACCESS_TOKEN";

    //删除客服
    private String kfaccount_delete_url="https://api.weixin.qq.com/customservice/kefu/del?access_token=ACCESS_TOKEN&kf_account=KFACCOUNT";

    //设置客服帐号的头像
    //暂时不做。

    //获取所有客服账号
    private String kfaccount_get_url="https://api.weixin.qq.com/cgi-bin/customservice/getkflist?access_token=ACCESS_TOKEN";


    @Override
    public void add(Kefu kefu, String accessToken) {

        // 拼装添加客服的url
        String url = kfaccount_add_url.replace("ACCESS_TOKEN", accessToken);
        // 将客服对象转换成json字符串
        String jsonKefu = JSONObject.fromObject(kefu).toString();
        // 调用接口
        JSONObject jsonObject = WeixinUtil.httpRequest(url, "POST", jsonKefu);

        if (null != jsonObject) {
            if (0 != jsonObject.getInt("errcode")) {
                log.error("添加客服失败 errcode:{} errmsg:{}", jsonObject.getInt("errcode"), jsonObject.getString("errmsg"));
            }
        }else {
            log.error("jsonObject为空");
        }
    }

    @Override
    public void update(Kefu kefu, String accessToken) {
        // 拼装添加客服的url
        String url = kfaccount_update_url.replace("ACCESS_TOKEN", accessToken);
        // 将客服对象转换成json字符串
        String jsonKefu = JSONObject.fromObject(kefu).toString();
        // 调用接口
        JSONObject jsonObject = WeixinUtil.httpRequest(url, "POST", jsonKefu);

        if (null != jsonObject) {
            if (0 != jsonObject.getInt("errcode")) {
                log.error("编辑客服失败 errcode:{} errmsg:{}", jsonObject.getInt("errcode"), jsonObject.getString("errmsg"));
            }
        }else {
            log.error("jsonObject为空");
        }
    }

    @Override
    public void delete(String kf_account,String accessToken) {
        // 拼装删除客服的url
        String url = kfaccount_delete_url.replace("ACCESS_TOKEN", accessToken).replace("KFACCOUNT",kf_account);

        // 调用接口
        JSONObject jsonObject = WeixinUtil.httpRequest(url, "GET", null);

        if (null != jsonObject) {
            if (0 != jsonObject.getInt("errcode")) {
                log.error("删除客服失败 errcode:{} errmsg:{}", jsonObject.getInt("errcode"), jsonObject.getString("errmsg"));
            }
        }else {
            log.error("jsonObject为空");
        }
    }

    @Override
    public JSONObject get(String accessToken) {
        // 拼装查询客服的url
        String url = kfaccount_get_url.replace("ACCESS_TOKEN", accessToken);

        // 调用接口
        JSONObject jsonObject = WeixinUtil.httpRequest(url, "GET", null);
        if (null != jsonObject) {
            if (0 != jsonObject.getInt("errcode")) {
                log.error("查询客服失败 errcode:{} errmsg:{}", jsonObject.getInt("errcode"), jsonObject.getString("errmsg"));
            }
        }else {
            log.error("jsonObject为空");
        }

        return jsonObject;
    }
}
